#include <REGX51.H>
#include "display.h"
#include "millis.h"
#include "delay.h"

#define REFRESHTIME 3

static unsigned long previousMillis = 0;

unsigned char code segmentsTable[]={0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7F, 0x6F, 0x76, 0x79, 0x38}; // 0-9, H, E, L
unsigned char code positionSelectionTable[] = {0x20, 0x10, 0x08, 0x04, 0x02, 0x01};

unsigned char positionSelection[6]=0;   //change this value
unsigned char displayBuffer[6] = 0;  //change this value


void refreshDisplay()
{
	static unsigned char pos = 0;
	volatile unsigned long currentMillis = millis();
	if(currentMillis-previousMillis>REFRESHTIME)
	{
		previousMillis = currentMillis;
		displaySingle(pos, displayBuffer[pos]);
		pos++;
		if(pos>5) pos=0;
	}
}

void displaySingle(unsigned char position, unsigned char num)
{		
	P2_0 = 1;
	P0 = segmentsTable[num];
	P2_0 = 0;
	
	P2_1 = 1;
	P0 = positionSelection[position];
	P2_1 = 0;
}

void displayString(unsigned char *str)
{
	unsigned char i = 0, length;
	unsigned char *pStr = str;
	
	while((*pStr++) != '\0') i++;
	length = i;
	if(length>6) length = 6;
	str = str + length - 1;
	
	for(i=5; i>5-length; i--)
	{
		displayBuffer[i]=(*str--)-'0';
		positionSelection[i] = positionSelectionTable[i];
	}
	for(i=0; i<=5-length; i++)
	{
		positionSelection[i] = 0;
	}
}

void displayNumberZeros(unsigned long num, bool showZeros)
{
	displayBuffer[0] = num/100000;
	displayBuffer[1] = num%100000/10000;
	displayBuffer[2] = num%10000/1000;
	displayBuffer[3] = num%1000/100;
	displayBuffer[4] = num%100/10;
	displayBuffer[5] = num%10;
	
	if(!showZeros)
	{
		unsigned int i;
		for(i=0; i<6; i++)
		{
			if(displayBuffer[i]!=0) break;
			positionSelection[i] = 0;
		}
		for( ;i<6; i++)
		{
			positionSelection[i] = positionSelectionTable[i];
		}
	}
	else
		resetPositionSelectionTable();
}

void displayNumberLength(unsigned long num, unsigned char length)
{
	unsigned int i,j;
	
	displayBuffer[0] = num/100000;
	displayBuffer[1] = num%100000/10000;
	displayBuffer[2] = num%10000/1000;
	displayBuffer[3] = num%1000/100;
	displayBuffer[4] = num%100/10;
	displayBuffer[5] = num%10;
	


	for(i=0; i<6; i++)
	{
		if(displayBuffer[i]!=0) break;  //Calculate the number of non-zero number
	}
	
	if(length >= (6-i))
	{
		for(j=0; j<6-length; j++)
		{
			positionSelection[j] = 0;
		}
		for(j=6-length; j<6; j++)
		{
			positionSelection[j] = positionSelectionTable[j];
		}
	}
	else
	{
		for(j=0; j<i; j++)
		{
			positionSelection[j] = 0;
		}
		for(j=i; j<6; j++)
		{
			positionSelection[j] = positionSelectionTable[j];
		}
	}
}

void resetPositionSelectionTable()
{
	positionSelection[0] = positionSelectionTable[0];
	positionSelection[1] = positionSelectionTable[1];
	positionSelection[2] = positionSelectionTable[2];
	positionSelection[3] = positionSelectionTable[3];
	positionSelection[4] = positionSelectionTable[4];
	positionSelection[5] = positionSelectionTable[5];
	
}

void displayClose()
{
	unsigned char i;
	for(i=0; i<6; i++)
	{
		positionSelection[i] = 0;
	}
}

void displayHello()
{
	displayBuffer[0] = 0; //show O
	displayBuffer[1] = 0; 
	displayBuffer[2] = 0; 
	displayBuffer[3] = 0; 
	displayBuffer[4] = 0;
	displayBuffer[5] = 0;
	positionSelection[0] = positionSelectionTable[0];
	positionSelection[1] = 0;
	positionSelection[2] = 0;
	positionSelection[3] = 0;
	positionSelection[4] = 0;
	positionSelection[5] = 0;
	delay(200);
	
	
	displayBuffer[0] = 12; 
	displayBuffer[1] = 0; 
	displayBuffer[2] = 0; 
	displayBuffer[3] = 0; 
	displayBuffer[4] = 0;  
	displayBuffer[5] = 0; 
	positionSelection[0] = positionSelectionTable[0];
	positionSelection[1] = positionSelectionTable[1];
	positionSelection[2] = 0;
	positionSelection[3] = 0;
	positionSelection[4] = 0;
	positionSelection[5] = 0;
	delay(200);
	
	displayBuffer[0] = 12;
	displayBuffer[1] = 12;
	displayBuffer[2] = 0;
	displayBuffer[3] = 0; 
	displayBuffer[4] = 0;
	displayBuffer[5] = 0;
	positionSelection[0] = positionSelectionTable[0];
	positionSelection[1] = positionSelectionTable[1];
	positionSelection[2] = positionSelectionTable[2];
	positionSelection[3] = 0;
	positionSelection[4] = 0;
	positionSelection[5] = 0;
	delay(200);
	
	displayBuffer[0] = 11;
	displayBuffer[1] = 12;
	displayBuffer[2] = 12;
	displayBuffer[3] = 0;
	displayBuffer[4] = 0;
	displayBuffer[5] = 0;
	positionSelection[0] = positionSelectionTable[0];
	positionSelection[1] = positionSelectionTable[1];
	positionSelection[2] = positionSelectionTable[2];
	positionSelection[3] = positionSelectionTable[3];
	positionSelection[4] = 0;
	positionSelection[5] = 0;
	delay(200);
	
	displayBuffer[0] = 10; 
	displayBuffer[1] = 11; 
	displayBuffer[2] = 12; 
	displayBuffer[3] = 12;
	displayBuffer[4] = 0; 
	displayBuffer[5] = 0; 
	positionSelection[0] = positionSelectionTable[0];
	positionSelection[1] = positionSelectionTable[1];
	positionSelection[2] = positionSelectionTable[2];
	positionSelection[3] = positionSelectionTable[3];
	positionSelection[4] = positionSelectionTable[4];
	positionSelection[5] = 0;
	delay(200);
}
	
